using System;
using System.Collections.Generic;

// 学生类
class Student
{
    // 属性
    public string Name { get; set; }
    public int StudentId { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }

    // 构造函数
    public Student(string name, int studentId, int age, string gender)
    {
        Name = name;
        StudentId = studentId;
        Age = age;
        Gender = gender;
    }

    // 重写ToString方法，方便输出学生信息
    public override string ToString()
    {
        return $"姓名: {Name}, 学号: {StudentId}, 年龄: {Age}, 性别: {Gender}";
    }
}

// 班级类，实现按索引访问学生
class Class
{
    // 属性
    public string ClassName { get; set; }
    public string Teacher { get; set; }
    private List<Student> students; // 存储学生的集合

    // 构造函数
    public Class(string className, string teacher)
    {
        ClassName = className;
        Teacher = teacher;
        students = new List<Student>();
    }

    // 索引器，实现按索引访问学生
    public Student this[int index]
    {
        get
        {
            if (index < 0 || index >= students.Count)
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
            return students[index];
        }
        set
        {
            if (index < 0 || index >= students.Count)
            {
                throw new IndexOutOfRangeException("索引超出范围");
            }
            students[index] = value;
        }
    }

    // 添加学生方法
    public void AddStudent(Student student)
    {
        students.Add(student);
    }

    // 移除学生方法
    public bool RemoveStudent(int studentId)
    {
        for (int i = 0; i < students.Count; i++)
        {
            if (students[i].StudentId == studentId)
            {
                students.RemoveAt(i);
                return true;
            }
        }
        return false;
    }

    // 获取学生数量
    public int StudentCount
    {
        get { return students.Count; }
    }

    // 查找学生
    public Student FindStudent(int studentId)
    {
        foreach (Student student in students)
        {
            if (student.StudentId == studentId)
            {
                return student;
            }
        }
        return null;
    }

    // 显示所有学生信息
    public void DisplayAllStudents()
    {
        Console.WriteLine($"班级: {ClassName}, 班主任: {Teacher}");
        Console.WriteLine($"学生总数: {students.Count}");
        Console.WriteLine("学生列表:");
        for (int i = 0; i < students.Count; i++)
        {
            Console.WriteLine($"[{i}] {students[i]}");
        }
    }
}

// 主程序类，用于测试
class Program
{
    static void Main(string[] args)
    {
        // 创建一个班级
        Class myClass = new Class("计算机科学与技术1班", "张老师");

        // 添加学生
        myClass.AddStudent(new Student("张三", 1001, 20, "男"));
        myClass.AddStudent(new Student("李四", 1002, 19, "女"));
        myClass.AddStudent(new Student("王五", 1003, 21, "男"));
        myClass.AddStudent(new Student("赵六", 1004, 19, "女"));

        // 显示所有学生
        Console.WriteLine("=== 初始班级信息 ===");
        myClass.DisplayAllStudents();

        // 使用索引器访问学生
        Console.WriteLine("\n=== 使用索引器访问学生 ===");
        for (int i = 0; i < myClass.StudentCount; i++)
        {
            Console.WriteLine($"索引 {i} 的学生: {myClass[i]}");
        }

        // 使用索引器修改学生信息
        Console.WriteLine("\n=== 使用索引器修改学生信息 ===");
        myClass[1] = new Student("李四改", 1002, 20, "女");
        Console.WriteLine($"修改后的索引 1 的学生: {myClass[1]}");

        // 测试边界情况
        Console.WriteLine("\n=== 测试索引边界 ===");
        try
        {
            Console.WriteLine(myClass[10]); // 应该抛出异常
        }
        catch (IndexOutOfRangeException ex)
        {
            Console.WriteLine($"捕获异常: {ex.Message}");
        }

        // 测试查找和移除学生
        Console.WriteLine("\n=== 测试查找和移除学生 ===");
        Student foundStudent = myClass.FindStudent(1003);
        if (foundStudent != null)
        {
            Console.WriteLine($"找到学生: {foundStudent}");
        }

        bool removed = myClass.RemoveStudent(1003);
        Console.WriteLine($"移除学号为1003的学生: {(removed ? "成功" : "失败")}");

        // 再次显示所有学生
        Console.WriteLine("\n=== 更新后的班级信息 ===");
        myClass.DisplayAllStudents();

        // 保持控制台窗口打开
        Console.WriteLine("\n按任意键退出...");
        Console.ReadKey();
    }
}